package com.bw.controller;

import com.alibaba.excel.EasyExcel;
import com.bw.entity.Bing;
import com.bw.entity.Dict;
import com.bw.entity.Zxt;
import com.bw.mapper.PeopleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

@Controller
public class TuController {

    @Autowired
    PeopleMapper peopleMapper;


    @RequestMapping("/tu.do")
    public String tu(Model model){
      List<Dict>  sourceList = peopleMapper.listSource();
        model.addAttribute("sourceList",sourceList);
        List<Dict>  statusList = peopleMapper.listStatus();
        model.addAttribute("statusList",statusList);
        return "tu";
    }


    @RequestMapping("/bing.do")
    @ResponseBody
    public List<Bing> bing(Integer sourceId, Integer statusId){

        return  peopleMapper.bing(sourceId,statusId);
    }


    @RequestMapping("/zxt.do")
    @ResponseBody
    public List<Zxt> zxt(Integer sourceId, Integer statusId){

        return  peopleMapper.zxt(sourceId,statusId);
    }


    @RequestMapping("/excel.do")
    public void excel(Integer sourceId, Integer statusId, HttpServletResponse response) throws IOException {
        List<Bing> bing = peopleMapper.bing(sourceId, statusId);

        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("人才信息统计表", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), Bing.class).sheet("sheet页1").doWrite(bing);

    }


}
